﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ServiceModel;
using landlord2lib;

namespace landlord2server
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class GameClient : IClient
    {
        private IClientCallback m_callback;

        private Guid m_guid;
        private bool m_faulted;
        private int m_currentWindow;

        public void Regist(Guid guid, string name, int gender)
        {
            m_guid = guid;
            m_callback = OperationContext.Current.GetCallbackChannel<IClientCallback>();
            OperationContext.Current.Channel.Closed += Channel_Closed;
            OperationContext.Current.Channel.Faulted += Channel_Faulted;

            GameHost.Current.Regist(this, name, gender);
        }

        private void Channel_Faulted(object sender, EventArgs e)
        {
        }

        private void Channel_Closed(object sender, EventArgs e)
        {
            GameHost.Current.Unregist(this);
        }

        public void SetCurrentWindow(int index)
        {
            m_currentWindow = index;
        }

        public DataSet GetTableList()
        {
            return GameHost.Current.GetTableList();
        }

        public DataSet SetSeat(int table, int seat)
        {
            return GameHost.Current.SetSeat(this, table, seat);
        }

        public DataSet ClearSeat()
        {
            return GameHost.Current.ClearSeat(this);
        }

        public DataSet GetPlayerInfo()
        {
            return GameHost.Current.GetPlayerInfo(this);
        }

        public void SetReady()
        {
            GameHost.Current.SetReady(this);
        }

        public void CallLord(int call)
        {
            GameHost.Current.CallLord(this, call);
        }

        public bool SendCard(Card[] cards, bool end)
        {
            return GameHost.Current.SendCard(this, cards, end);
        }

        public void HeartBeat()
        {

        }

        public void SendChat(int gender, int index)
        {
            GameHost.Current.SendChat(this, gender, index);
        }

        public void SendChatText(string text)
        {
            GameHost.Current.SendChatText(this, text);
        }

        public void RobotPlay(bool ena)
        {
            GameHost.Current.RobotPlay(this, ena);
        }

        public IClientCallback GetCallback()
        {
            return m_callback;
        }

        public Guid Guid
        {
            get
            {
                return m_guid;
            }
        }

        public bool Faulted
        {
            get
            {
                return m_faulted;
            }
            set
            {
                m_faulted = value;
            }
        }

        public int CurrentWindow
        {
            get
            {
                return m_currentWindow;
            }
        }
    }
}
